import {
  Body,
  Controller,
  Delete,
  Get,
  Post,
  Put,
  Query,
  Request,
  UseGuards,
  ValidationPipe
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth/auth.service';
import { CreateUserDto } from './users/dto/create.user.dto';
import { UsersService } from './users/users.service';
import { createPublicKey } from 'crypto';
const NodeRSA = require('node-rsa');

@Controller()
export class AppController {
  constructor(
    private readonly authService: AuthService,
    private readonly userService: UsersService,
  ) { }

  @Post('auth/login')
  login(@Request() req) {
    return this.authService.login(req.body);
  }

  

  @Post('user/add')
  createUser(@Body(new ValidationPipe()) createUserDto: CreateUserDto) {
    return this.userService.add(createUserDto);
  }

  @UseGuards(AuthGuard('jwt'))
  @Delete('user/delete')
  delete(@Request() req) {
    return this.userService.delete(req.body.id);
  }

  @UseGuards(AuthGuard('jwt'))
  @Put('user/update')
  update(@Request() req) {
    return this.userService.update(req.body.id, req.body);
  }

  @UseGuards(AuthGuard('jwt'))
  @Get('user')
  index(@Query() query: any) {
    return this.userService.findAll(query);
  }



  @Get('test')
  test(@Query() query: any) {
    const zhengshu = `-----BEGIN CERTIFICATE-----\n MIID3DCCAsSgAwIBAgIUdtpEy2VPNBA59o4erGyyxOKaq1AwDQYJKoZIhvcNAQEL BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT FFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3Qg Q0EwHhcNMjIwNzI4MDM1ODU4WhcNMjcwNzI3MDM1ODU4WjBuMRgwFgYDVQQDDA9U ZW5wYXkuY29tIHNpZ24xEzARBgNVBAoMClRlbnBheS5jb20xHTAbBgNVBAsMFFRl bnBheS5jb20gQ0EgQ2VudGVyMQswCQYDVQQGDAJDTjERMA8GA1UEBwwIU2hlblpo ZW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCmn8zdSnNBvUsfCM7z f0ZgB+tN1H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RDK1dCIMj2LaHyFADG/HmphrId4c3wi1znxxI4SpqWdLjH8aUzQz/YKV4eP+pd/ylt/WP2f3U6shFrV/EsHL74v8PfsshNtMVRpXc/P3yI02FnfpX/e37wVCJZWqsTzontCpY/rfYdLMWb1Hl/e2o0Bpk4Rnt2d0H9VSiEyq9bgf3IFHthzZfKw==`;
    const buffer = Buffer.from(sgin, 'base64');
    sgin = buffer.toString('utf-8')//解码后应答签名
    console.log('=================sgin===================');
    console.log(sgin);
    console.log('====================================');
    const certificate_public_key = createPublicKey(zhengshu).export({ type: 'pkcs1', format: 'pem' });
    const key = new NodeRSA(certificate_public_key, 'pkcs1-public');
    const str = `1659336056p1qupzQNNorIjUsUtMP2BuZh76TeVao3{"id":"c42ed0f9-8175-5d40-90ad-6413eee960f5","create_time":"2022-08-01T11:35:55+08:00","resource_type":"encrypt-resource","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"original_type":"transaction","algorithm":"AEAD_AES_256_GCM","ciphertext":"QQJDw3xW6EPRJWLPD+7826Noi3TAVtHOT26a9UYhvCWbypgOiSGL8oJernbNbYkkle8hzT205zm/daLcTXfzLgD+HKB/rdEXRlFhN9mR9N+1ZWPdAWZ3k4oHLT0buZ4NDnoH4igl3Z67kdGzapbC0v9Jzuiis/lyJn0/fPp6cCOEBM1fEKT/6Lyiu+kPMCns1Os2yGzoQlkpktan4tSjdx9lZ6T9sD7I3/qS4eMxj7Q9lGt/0mk6sDKStULYVxJ/OkvvOXFZdxQYqLM9WkfoZwhi34Hu+TnlHsRxjZ495vXjytJkk6TsyUiR+raN+uHulwEmMBiYlV6obNDrF6kJ4/CxnWkYy7hjuaoPaOYlXU5sXWhAVv0LvA3u7fA/5DCRCP1oSV9WEmt79I4UugiBhsQqnCTW3Ee35SnT7eIV35MIlpvLd1M9fGDnIWozvQagXcICOtugZvzAhaNYQnU5Kcx/pSI34ccQBVaUBA698OgRYazmGYvCxLnXUfhslDQ58RHkjhuRfxADHTLd14JyAxoAIDgGAAUwZq1Ni0liFlKDUrfltSnuRjEYyMWHsrp7mujTDA==","associated_data":"transaction","nonce":"RX5eGVPxZYvu"}}`;
    const result = key.verify(str, sgin);

    return result;
  }

}